package com.budwk.app.sqfwV2.service.publicSecurity;


import com.alibaba.fastjson.JSONObject;
import com.budwk.app.sqfw.enums.SupportBusinessTypeEnum;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfw.services.SqfwFormSummaryService;
import com.budwk.app.sqfw.services.SqfwFormsummaryNewService;
import com.budwk.app.sqfw.vo.GenerateExcelUrlVO;
import com.budwk.app.sqfwDoc.bo.ExcelCreateConfig;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.BaseExcelParamDTO;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.ExcelGeneratorConfig;
import com.budwk.app.sqfwDoc.codeGenerator.impl.SqfwTemplateExcelCommonService;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.AbstractCustomerPdfCreateField;
import com.budwk.app.sqfwV2.dto.publicSecurity.PublicSecurityDTO;
import com.budwk.app.sqfwV2.enums.publicSecurity.PublicSecurityFileEnum;
import com.budwk.app.sqfwV2.generator.publicSecurity.exce.PublicSecurityExcelGenerator;
import com.budwk.app.sys.models.Sys_user;
import com.budwk.app.sys.services.SysUserService;
import com.budwk.app.util.poiUtils.nutzBeanUtils.NutzIocUtils;
import com.budwk.starter.security.utils.SecurityUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 东方祥云低代码1.1
 * @since 2024-03-25
 * <p>
 * 大营路社会治安 1
 */
@IocBean
public class PublicSecurityExcelService extends SqfwTemplateExcelCommonService {

    @Inject
    private SqfwFormsummaryNewService sqfwFormsummaryNewService;

    @Inject
    private SqfwFormSummaryService sqfwFormsummaryService;

    @Inject
    private SysUserService userService;

    @Override
    public List<GenerateExcelUrlVO> generateExcel(List<ExcelCreateConfig> excelConfigList, String userId, List<String> sqfwIdList) {
        return super.generateExcel(excelConfigList, userId, sqfwIdList);
    }

    @Override
    protected void customerPopularBaseExcelParam(BaseExcelParamDTO baseExcelParam, String userId, List<String> sqfwIdList) {

    }

    @Override
    protected void customerPopularBaseExcelParamForAdmin(BaseExcelParamDTO baseExcelParam, String userId, List<String> formSummaryNewIdList) {

    }

    @Override
    protected List<ExcelGeneratorConfig> getExcelCodeGeneratorConfig(List<ExcelCreateConfig> excelConfigList, BaseExcelParamDTO baseExcelParam) {
        List<ExcelGeneratorConfig> list = new ArrayList<>();
        for (ExcelCreateConfig config : excelConfigList) {
            ExcelGeneratorConfig generatorConfig = new ExcelGeneratorConfig();
            PublicSecurityFileEnum publicSecurityFileEnum = PublicSecurityFileEnum.getPublicSecurityFileByCode(config.getFileIdentity());
            String template = PublicSecurityFileEnum.PUBLIC_SECURITY_STANDING_BOOK.getTemplatePath();
            if (ObjectUtils.isNotEmpty(publicSecurityFileEnum)) {
                template = publicSecurityFileEnum.getTemplatePath();
            }
            generatorConfig.setGenerator(new PublicSecurityExcelGenerator(template));
            generatorConfig.setExcelCreateConfig(config);
            list.add(generatorConfig);
        }
        return list;
    }

    @Override
    protected AbstractCustomerPdfCreateField parseParamData(String paramDataStr) {
        return JSONObject.parseObject(paramDataStr, PublicSecurityDTO.class);
    }

    @Override
    protected String getSqfwFormSummaryType() {
        return SupportBusinessTypeEnum.PUBLIC_SECURITY.getName();
    }

    protected List<Sqfw_formSummary_new> queryForSummaryData(List<String> sqfwIdList, Map<String, Object> param) {
        Cnd cnd = Cnd.NEW();
        //新版观山湖app 2.0走这个条件
        if (param.containsKey("fillStartDate")) {
            if (ObjectUtils.isNotEmpty(param.get("fillStartDate"))) {
                cnd.and(Sqfw_formSummary_new::getFillDate, ">", String.format("%s 00:00:00", param.get("fillStartDate")));
            }
            if (ObjectUtils.isNotEmpty(param.get("fillEndDate"))) {
                cnd.and(Sqfw_formSummary_new::getFillDate, "<", String.format("%s 23:59:59", param.get("fillEndDate")));
            }

            if (ObjectUtils.isNotEmpty(param.get("shopInfoId"))) {
                cnd.and(Sqfw_formSummary_new::getShopsBaseInfoId, "=", param.get("shopInfoId"));
            }
            Sys_user user = userService.getUserById(SecurityUtil.getUserId());
            //拼接五级 省
            cnd.and(Sqfw_formSummary_new::getProvince, "=", user.getProvince());
            //拼接五级 市
            cnd.and(Sqfw_formSummary_new::getCity, "=", user.getCity());
            //拼接五级 区
            cnd.and(Sqfw_formSummary_new::getDistrict, "=", user.getDistrict());
            //拼接五级 街道
            cnd.and(Sqfw_formSummary_new::getCounty, "=", user.getCounty());
            //拼接五级 社区
            cnd.and(Sqfw_formSummary_new::getCommunity, "=", user.getCommunity());
        } else {
            cnd.and(Sqfw_formSummary_new::getSqfwId, "in", sqfwIdList);
        }
        cnd.and("type", "=", getSqfwFormSummaryType());
        cnd.orderBy("fill_date", "asc");
        List<Sqfw_formSummary_new> sqfwFormSummaryNews = sqfwFormsummaryNewService.query(cnd);
        return sqfwFormSummaryNews;
    }

    @Override
    protected List<Sqfw_formSummary_new> queryForSummaryDataForAdmin(List<String> formSummaryNewIdList) {
        SqfwFormsummaryNewService sqfwFormsummaryNews = NutzIocUtils.getBean(SqfwFormsummaryNewService.class);
        Cnd cnd = Cnd.NEW();
        cnd.and(Sqfw_formSummary_new::getId, "in", formSummaryNewIdList).and("type", "=", SupportBusinessTypeEnum.PUBLIC_SECURITY.getName());
        cnd.orderBy("fill_date", "asc");
        return sqfwFormsummaryNews.query(cnd);
    }
}
